<?php
class Alogin {
	public static function main($funcode) {
		sview::verify();
		switch ($funcode) {
			case 1:
				self::__dologin();
				break;
			case 2:
				self::__doretrive();
				break;
			case 3:
				self::__dofblogin();
				break;
			case 4:
				self::__dosignout();
				break;
			case 5:
				self::__doresetpwd();
				break;
			default:
				sview::$view->assign_error(404, lang::text(404));
		}
	}
	
	//会员登录验证1-1-1
	private static function __dologin() {
		sview::$ctrl['account'] = trim(sview::$ctrl['account']);
		sview::$ctrl['loginpw'] = trim(sview::$ctrl['loginpw']);
		$psocial = null;
		//账号是邮箱的话使用邮箱登录
		if(verify::is_email(sview::$ctrl['account'])) {
			$res   = api_uc::login(sview::$ctrl['account'], sview::$ctrl['loginpw']);
			if ($res['result'] != '000') {
				sview::$view->assign_error(80005, lang::text(80005));
			}
			$psocial = $res['value'];
		} else {
			sview::$view->assign_error(80001, lang::text(80001));
		}
		//处理资料信息的同步更新
		$user  = api_uc::sync_user($psocial, sview::$ctrl['loginpw']);
		if ($user['status'] != 1) {
			sview::$view->assign_error(80008, lang::text(80008));
		}
		//记录登录日志资料信息
		$ip    = get_ip();
		$acl   = acl::getinst('vedio');
		$field = array('userid'=>$user['userid'], 'account'=>sview::$ctrl['account'], 'logip'=>$ip,
				'version'=>'app1.0', 'os'=>$_SERVER['HTTP_USER_AGENT'], 'stime'=>config::$nowtime);
		model::load('live/loginlog')->new_one($field);
		$acl->set_loginpw($user['userid'], md5(sview::$ctrl['loginpw'].config::$nowtime), 0);
		$luser = array('id'=>$user['userid'], 'st'=>substr(config::$nowtime, 5));
		if (!$psocial['emailVerifyYn']) {
			$luser['sg'] = 0;
		}
		Amain::onesignal($user['userid']);
		$anchor= model::load('live/anchor')->get_one($user['userid']);
		$roler = empty($anchor)? 0:1;
		$data  = array('signature'=>$acl->setcrypt($luser), 'userid'=>$user['userid'], 'roler'=>$roler);
		sview::$view->assign_by_ref('datasets', $data);
		sview::$view->display();
	}
	
	//会员找回密码1-1-2
	private static function __doretrive() {
		$email = trim(sview::$ctrl['account']);
		if (!verify::is_email($email)) {
			sview::$view->assign_error(80018, lang::text(80018));
		}
		//通过邮箱获取userId 和 Token信息
		$res   = api_uc::getbyemail($email);
		if ($res['result'] != '000') {
			sview::$view->assign_error(80027, lang::text(80027));
		}
		$psocial = api_uc::getuser($res['value']['token']);
		if (!$psocial || $psocial['result']!= '000') {
			sview::$view->assign_error(80006, lang::text(80006));
		}
		if ($res['value']['token'] && !$psocial['value']['token']) {
			$psocial['value']['token'] = $res['value']['token'];
		}
		//信息更新到PShow
		$user    = api_uc::sync_user($psocial['value'], randme::rand_str(6));
		$queue   = sprintf('ems_%d', $user['userid']);
		if (!cache::getinst()->get($queue)) {
			$vcode  = api_vcode::setmailcode($email, 2, $user['userid']);
			$skey   = sprintf('%s;%s', $vcode, $email);
			$skey   = xxtea::encrypt($skey);
			$link   = router::get_url('retrieve', 'emailcheck').'?s='.urlencode($skey);
			$link   = router::get_base_url().$link;
			//获取发送的模板信息
			$data   = api_mailer::tpl_compiler(array('nickname'=>$user['nickname'], 'url'=>$link), 50005);
			if (!$data) {
				sview::$view->assign_error(80009, lang::text(80009));
			}
			api_mailer::send($data['title'], $data['tpl'], $email);
			cache::getinst()->set($queue, 1, api_mailer::EMAILEXPIRE);
		}
		sview::$view->display();
	}
	
	//1-1-3 FB第三方登录/注册
	private static function __dofblogin() {
		$fbtoken = trim(sview::$ctrl['fbToken']);
		$res     = api_uc::fb_login($fbtoken);
		if ($res['result'] != '000') {
			sview::$view->assign_error(60007, $res['message']);
		}
		//处理资料信息的同步更新
		$loginpw = randme::rand_str(6);
		$user    = api_uc::sync_user($res['value'], $loginpw, 1);
		if ($user['status'] != 1) {
			sview::$view->assign_error(80008, lang::text(80008));
		}
		api_uc::snstoken($user['userid'], $fbtoken);
		//记录登录日志资料信息
		$field   = array('userid'=>$user['userid'], 'account'=>$res['value']['email'], 'logip'=>get_ip(),
				'version'=>'App-FB1.0', 'os'=>$_SERVER['HTTP_USER_AGENT'], 'stime'=>config::$nowtime);
		model::load('live/loginlog')->new_one($field);
		//设置写入登录的cookie资料信息
		$acl   = acl::getinst('vedio');
		$luser = array('id'=>$user['userid'], 'st'=>substr(config::$nowtime, 5));
		if (!$res['value']['emailVerifyYn']) {
			$luser['sg'] = 0;
		}
		$acl->set_loginpw($user['userid'], $loginpw);
		//活动发星币
		Amain::onesignal($user['userid']);
		api_activity::sendcoin($user);
		$anchor= model::load('live/anchor')->get_one($user['userid']);
		$roler = empty($anchor)? 0:1;
		$data  = array('signature'=>$acl->setcrypt($luser), 'userid'=>$user['userid'], 'roler'=>$roler);
		sview::$view->assign_by_ref('datasets', $data);
		sview::$view->display();
	}
	
	//注销会员1-1-4
	private static function __dosignout() {
		$acl   = acl::getinst('vedio');
		if ($acl->user) {
			Amain::onesignal($acl->user['id']);
			$acl->setlogout(false);
		}
		sview::$view->display();
	}
	
	//修改密码1-1-5
	private static function __doresetpwd() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$orm    = model::load('live/userinfo')->set_devision($userid);
		$user   = $orm->get_one($userid);
		if (!$user['emailverify']) {
			sview::$view->assign_error(60025, lang::text(60025));
		}
		$newpwd = trim(sview::$ctrl['newpwd']);
		if (!verify::is_pwd($newpwd)) {
			sview::$view->assign_error(60017, lang::text(60017));
		}
		//判断登录密码是否设置
		if ($user['isthirdset']) {
			$oldpwd = trim(sview::$ctrl['oldpwd']);
			$res    = api_uc::login($user['email'], $oldpwd);
			if ($res['result'] != '000') {
				sview::$view->assign_error(80023, lang::text(80023));
			}
			if($oldpwd == $newpwd) {
				sview::$view->assign_error(80025, lang::text(80025));
			}
		}
		$res    = api_uc::save($user['token'], null, $newpwd);
		if ($res['result'] != '000') {
			sview::$view->assign_error(60023, lang::text(60023));
		}
		api_uc::sync_user($res['value'], $newpwd);
		//设置密码已修改
		$orm->save($userid, array('isthirdset'=>1));
		$acl->set_loginpw($userid, md5($newpwd));
		
		Amain::onesignal($userid);
		
		//修改了密码发送邮箱提示
		$one   = model::load('live/usersetting')->set_devision($userid)->get_one($userid);
		if(!$one || (intval($one['mailnotify']) & 0x01)) {
			$vcode  = api_vcode::setmailcode($user['email'], 2, $userid);
			$skey   = sprintf('%s;%s', $vcode, $user['email']);
			$skey   = xxtea::encrypt($skey);
			$link   = router::get_url('retrieve', 'emailcheck').'?s='.urlencode($skey);
			$link   = router::get_base_url().$link;
			$data = api_mailer::tpl_compiler(array('nickname'=>$user['nickname'], 'url'=>$link), 50013);
			api_mailer::send($data['title'], $data['tpl'], $user['email']);
		}
		sview::$view->display();
	}
}